//
//  WCNSecondBodyPartVC.m
//  WCN
//
//  Created by 杨鑫 on 15/6/26.
//  Copyright (c) 2015年 ShanghaiCize Trade And Business Co., Ltd. All rights reserved.
//

#import "WCNSecondBodyPartVC.h"
#import "WCNAddCustomPartVC.h"
//
#import "MJRefresh.h"
//
#import "Patient.h"
#import "WCNetworkAccessManager.h"
//
#import "Macros.h"

@interface WCNSecondBodyPartVC ()<UITableViewDataSource, UITableViewDelegate>

@property (strong, nonatomic) NSDictionary *part;

@property (weak, nonatomic) IBOutlet UILabel *firstBodyPartName;
@property (weak, nonatomic) IBOutlet UITableView *tableView;
@property (weak, nonatomic) IBOutlet UIButton *customPart;

@property (strong, nonatomic) NSArray *bodyParts;

@end

@implementation WCNSecondBodyPartVC

- (instancetype)initWithFirstPart:(NSDictionary *)part
{
    self = [super init];
    if (self) {
        self.title = @"伤口部位";
        
        _part = part;
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib
    self.firstBodyPartName.text = self.part[@"partName"];
    self.customPart.layer.cornerRadius = 3;
    
    self.tableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, 70)];
    [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"UITableViewCell"];
    
    __weak typeof(self) weakSelf = self;
    [self.tableView addLegendHeaderWithRefreshingBlock:^{
        [weakSelf refresh];
    }];
    [self.tableView.header beginRefreshing];
}

- (void)refresh
{
    NSString *url = [NSString stringWithFormat:@"wound/bodypart/second/%@/%@", self.part[@"partCode"], self.part[@"partType"]];
    //@{@"direction":self.part[@"direction"]}
    [[WCNetworkAccessManager sharedMg] GET:url parameters:nil success:^(NSDictionary *result){
        self.bodyParts = result[@"data"];
        [self.tableView reloadData];
    } failure:nil netError:nil always:^{
        [self.tableView.header endRefreshing];
    }];
}

#pragma mark - Nib Action

- (IBAction)toAddCustomPart:(id)sender {
    WCNAddCustomPartVC *vc = [[WCNAddCustomPartVC alloc] init];
    vc.wound = self.wound;
    vc.firstPart = self.part;
    [self.navigationController pushViewController:vc animated:YES];
}

#pragma mark - <UITableViewDataSource, UITableViewDelegate>

- (UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"UITableViewCell" forIndexPath:indexPath];
    
    NSDictionary *bodyPart = self.bodyParts[indexPath.row];
    cell.textLabel.text = bodyPart[@"partName"];
    
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    [cell setSeparatorInset:UIEdgeInsetsZero];
    if ([cell respondsToSelector:@selector(setPreservesSuperviewLayoutMargins:)]) {
        [cell setPreservesSuperviewLayoutMargins:NO];
    }
    if ([cell respondsToSelector:@selector(setLayoutMargins:)]) {
        [cell setLayoutMargins:UIEdgeInsetsZero];
    }
    
    return cell;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.bodyParts.count;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    NSDictionary *data = self.bodyParts[indexPath.row];
    self.wound[@"bodyPartName"] = data[@"partName"];
    self.wound[@"bodyPartCode"] = data[@"partCode"];
    
    NSArray *vcs = self.navigationController.viewControllers;
    UIViewController *vc = vcs[vcs.count-3];
    [self.navigationController popToViewController:vc animated:YES];
}

@end
